package com.wing.product.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductType;
import com.wing.product.model.response.ProductTypeVO;
import com.wing.product.service.ProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.product.model.request.ProductTypeForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-产品类型管理", tags = "产品类型管理")
@RestController("appProductTypeController")
@RequestMapping("product/app/productType")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询所有产品类型", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", example = "desc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductTypeVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            , ProductType.Type type
    ) {
        Page<ProductTypeVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if(null != type) {
            params.put("type", type.getValue());
        }
        Page<ProductTypeVO> list = productTypeService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 查询所有刀具类型（无用）
     */
    @GetMapping("/getAllKnife")
    public JsonResult getAll(){
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("type", ProductType.Type.knife.getValue());
        wrapper.eq("data_source_type", ProductType.DataSourceType.platform.getValue());
        List<ProductType> list = productTypeService.list(wrapper);
        return JsonResult.success(list);
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long productTypeId) {
        ProductTypeVO productTypeVO = productTypeService.get4appById(productTypeId);
        if (productTypeVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), productTypeVO);
    }

    @PostMapping()
    public JsonResult add(@Validated ProductTypeForm productTypeForm) {
        if (productTypeService.add(productTypeForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
